<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <input type="text" class="username">
    <br><br>
    <input type="password" class="userpwd">
    <br><br>
    <input type="text" class="usertel">
    <br><br>
    <button>注册</button>


    <script>

        const oBtn = document.querySelector('button');
        const oUser = document.querySelector('.username');
        const oPwd = document.querySelector('.userpwd');
        const oTel = document.querySelector('.usertel');

        oBtn.onclick = function () {
            const username = oUser.value;
            const userpwd = oPwd.value;
            const usertel = oTel.value;
            if (username && userpwd && usertel) {
                const xhr = new XMLHttpRequest();
                xhr.open('post', `../php/reg.php`, true);
                xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
                xhr.send(`username=${username}&userpwd=${userpwd}&usertel=${usertel}`);
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4 && xhr.status === 200) {
                        let data = xhr.responseText;
                        console.log(data);
                        data = JSON.parse(data);
                        console.log(data);
                        const { status, msg } = data;
                        if (status) {
                            location.href = './03login.html';
                        } else {
                            alert(msg);
                        }
                    }
                }
            }
        }





    </script>

</body>

</html>